package com.geng.controller;

import com.geng.bean.Express;
import com.geng.bean.Message;
import com.geng.bean.ResultData;
import com.geng.mvc.ResponseBody;
import com.geng.service.ExpressService;
import com.geng.utils.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {
    ExpressService express = new ExpressService();
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //1.调用expressService,接收返回的参数
        List data = express.console();
        //创建Message对象，进行数据封装
        Message msg = new Message();
        if(data.size()!=0){
            msg.setStatus(0);
        }else{
            msg.setStatus(-1);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request,HttpServletResponse response){
        //1. 获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2. 获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行查询
        List<Express> all = express.findAll(true, offset, pageNumber);
        List<Map<String, Integer>> console = express.console();
        //4.将集合封装为 bootstrap-table 识别的格式
        ResultData<Express> data = new ResultData<>();
        data.setRows(all);
        data.setTotal(console.get(0).get("data1_size"));
        String json = JSONUtil.toJSON(data);
        return json;
    }
}
